package ru.sviridov.sellandbuy;
import ru.sviridov.sellandbuy.model.query.QueryGetAllCountry;
import ru.sviridov.sellandbuy.model.query.QueryGetCities;
import ru.sviridov.sellandbuy.model.query.QueryGetStates;
import ru.sviridov.sellandbuy.tasks.base.BaseTask;
import ru.sviridov.sellandbuy.tasks.recipients.RegionsRecipient;
import ru.sviridov.sellandbuy.tools.Constants;
import ru.sviridov.sellandbuy.url.APIGroup;
import ru.sviridov.sellandbuy.url.APIMethod;
import ru.sviridov.sellandbuy.url.URL;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import api.istsoft.ru.dal.Geo.City;
import api.istsoft.ru.dal.Geo.Country;
import api.istsoft.ru.dal.Geo.State;


public class ChooseAddressActivity extends Activity{

	public static final String CITY_ID = "ru.sviridov.sellandbuy.CITY_ID";
	public static final String STATE_ID = "ru.sviridov.sellandbuy.STATE_ID";
	public static final String COUNTRY_ID = "ru.sviridov.sellandbuy.COUNTRY_ID";
	public static final String CITY_NAME = "ru.sviridov.sellandbuy.CITY_NAME";
	public static final String STATE_NAME = "ru.sviridov.sellandbuy.STATE_NAME";
	public static final String COUNTRY_NAME = "ru.sviridov.sellandbuy.COUNTRY_NAME";
	private BaseTask<Void> task;
	private RegionsRecipient recipient;
	
	private Spinner spCountry;
	private Spinner spState;
	private Spinner spCity;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ContentViewMapper.setContentView(this);
		
		recipient = new RegionsRecipient(RegionsRecipient.COUNTRY);
		configViews();
		loadCountries();
	}

	private void configViews() {
		spCountry = (Spinner)findViewById(R.id.spinner_country);
		spState = (Spinner)findViewById(R.id.spinner_state);
		spCity = (Spinner)findViewById(R.id.spinner_city);
		
		spCountry.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				Country country = (Country)parent.getItemAtPosition(position);
				loadStates(country);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {}
		});
		spState.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				State state = (State)parent.getItemAtPosition(position);
				loadCities(state);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {}
		});

	}
	
	private void loadCountries() {
		if(task!=null && task.getStatus()!=AsyncTask.Status.FINISHED)
			task.cancel(true);
		
		QueryGetAllCountry query = new QueryGetAllCountry();
		query.setCode(1); //FIXME temporary language_code = 1(ru)
		
		task = new BaseTask<Void>(this, recipient);
		task.setQueryParams(query);
		task.execute(URL.getURL(APIGroup.GEO, APIMethod.GET_ALL_COUNTRY));
		
	}

	private void loadStates(Country country){
		if(task!=null && task.getStatus()!=AsyncTask.Status.FINISHED)
			task.cancel(true);
		
		QueryGetStates query = new QueryGetStates();
		query.setCode(1); //FIXME temporary language_code = 1(ru)
		query.setCountryId(country.getId());
		query.setRegionId(0);
		query.setStateTypes(new int[] {2,3,4});
		
		recipient.setRegionType(RegionsRecipient.STATE);
		
		task = new BaseTask<Void>(this, recipient);
		task.setQueryParams(query);
		task.execute(URL.getURL(APIGroup.GEO, APIMethod.GET_STATES));		
	}
	
	protected void loadCities(State state) {
		if(task!=null && task.getStatus()!=AsyncTask.Status.FINISHED)
			task.cancel(true);
		
		QueryGetCities query = new QueryGetCities();
		query.setCode(1L); //FIXME temporary language_code = 1(ru)
		query.setCityId(0L);
		query.setCountryId(state.getCountryId());
		query.setStateId(state.getId());
		query.setType(1L); //city type. 1 - just town
		
		recipient.setRegionType(RegionsRecipient.CITY);
		
		task = new BaseTask<Void>(this, recipient);
		task.setQueryParams(query);
		task.execute(URL.getURL(APIGroup.GEO, APIMethod.GET_CITIES));
	}
	
	public void setCountries(Country[] countries){
		ArrayAdapter<Country> adapter = new ArrayAdapter<Country>(this,android.R.layout.simple_dropdown_item_1line, countries);
		spCountry.setSelected(false);
		spCountry.setAdapter(adapter);		
	}

	public void setStates(State[] states) {
		ArrayAdapter<State> adapter = new ArrayAdapter<State>(this,android.R.layout.simple_dropdown_item_1line, states);
		spState.setAdapter(adapter);
	}
	
	public void setCities(City[] cities) {
		ArrayAdapter<City> adapter = new ArrayAdapter<City>(this,android.R.layout.simple_dropdown_item_1line, cities);
		spCity.setAdapter(adapter);
	}
	
	public void onClickFinish(View view){
		Country country = (Country)spCountry.getSelectedItem();
		State state = (State)spState.getSelectedItem();
		City city =((City)spCity.getSelectedItem());

		Editor editor = getSharedPreferences(Constants.USER_PREFS, Activity.MODE_PRIVATE).edit();
		country.store(editor);
		state.store(editor);
		city.store(editor);
		editor.commit();
		
		Intent data = new Intent();
		data.putExtra(CITY_ID, city.getId());
		data.putExtra(CITY_NAME, city.getName());
		data.putExtra(STATE_ID, state.getId());
		data.putExtra(STATE_NAME, state.getName());
		data.putExtra(COUNTRY_ID, country.getId());
		data.putExtra(COUNTRY_NAME, country.getName());
		setResult(RESULT_OK, data);
		finish();
	}
	
}
